<script setup>
import { ref } from 'vue';
import { ElCard, ElButton, ElRow, ElCol, ElInput } from 'element-plus';
import userInfoStore from '@/stores/userInfo.js'
import {allMessage,delectMessage,addMessage} from '@/api/message.js'
import { ElMessage } from 'element-plus'

const userStore=userInfoStore();

// 管理员权限
const isAdmin = ref(userStore.info.username=='zzq');

// 数据
const newMessage = ref('');
const messages = ref([

]);

const show=async ()=>{
       let result=await allMessage();
       messages.value=result.data;
    }
show()

// 提交留言
const submitMessage = async () => {
  if (newMessage.value.trim()) {
    const newMsg = {
      senderName: userStore.info.name,
      messageContent: newMessage.value,
    };
    
    let result=await addMessage(newMsg);
    if(result.code===200){
        ElMessage.success('发送成功')
        show()
    }else{
        ElMessage.success('发送失败')
    }
    newMessage.value = ''; // 清空输入框
  }
};

// 删除留言
const deleteMessage = async (messageId) => {
    let result=await delectMessage(messageId);
    if(result.code===200){
        ElMessage.success('删除成功')
        show()
    }else{
        ElMessage.success('删除失败')
    }
};
</script>

<template>
    <div class="message-board">
      <!-- 留言展示区域 -->
      <div class="message-list">
        <el-row class="message-row">
          <el-col :span="24">
            <el-card v-for="message in messages" :key="message.id" class="message-card" shadow="hover">
              <div>
                <p class="message-content">{{ message.messageContent }}</p>
                <small class="message-meta">By {{ message.senderName }} on {{ message.createAt }}</small>
                <!-- 只有管理员可以看到删除按钮 -->
                <el-button
                  v-if="isAdmin"
                  size="mini"
                  type="danger"
                  @click="deleteMessage(message.id)"
                  class="delete-btn"
                >
                  删除
                </el-button>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
  
      <!-- 留言输入框（固定在页脚） -->
      <div class="message-footer">
        <el-card class="input-card">
          <el-input
            v-model="newMessage"
            type="textarea"
            placeholder="请输入留言..."
            rows="4"
            show-word-limit
            maxlength="500"
            class="input-textarea"
          />
          <el-button
            type="primary"
            @click="submitMessage"
            :disabled="!newMessage"
            class="submit-btn"
          >
            提交留言
          </el-button>
        </el-card>
      </div>
    </div>
  </template>
  
  
  <style scoped>
  .message-board {
    display: flex;
    flex-direction: column;
    height: 100%;
    width:80%;
    padding: 20px;
    background-color: white;
    border-radius: 8px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  }
  
  .message-list {
    flex-grow: 1; /* 允许留言列表部分占据剩余空间 */
    overflow-y: auto; /* 内容超出时显示滚动条 */
    margin-bottom: 20px;
  }
  
  .message-row {
    margin-bottom: 15px;
  }
  
  .message-card {
    padding: 15px;
    border-radius: 8px;
    margin-bottom: 10px;
    background-color: #f9f9f9;
  }
  
  .message-content {
    font-size: 16px;
    line-height: 1.5;
  }
  
  .message-meta {
    font-size: 12px;
    color: #888;
    margin-right: 10px;
  }
  
  .input-card {
    padding: 20px;
    border-radius: 8px;
    background-color: #f9f9f9;
  }
  
  .input-textarea {
    width: 100%;
  }
  
  .submit-btn {
    width: 100%;
    margin-top: 15px;
  }
  
  .delete-btn {
    margin-top: 10px;
  }
  
  .message-footer {
    position: sticky;
    bottom: 0;
    background-color: white;
    padding: 20px;
    box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
  }
  </style>